import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/login.vue'
import Home from '../views/home.vue'

Vue.use(Router)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home }
]

const router = new Router({
  routes
})

export default router
router.beforeEach((to, from, next) => {
  const myToken = window.sessionStorage.getItem('token')
  if (to.path === '/login') return next()
  if (!myToken) return next('/login')
  next()
})
// 防止多次操作  导致报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
